package com.sc.nft.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.ExportRecordsDao;
import com.sc.nft.dao.TempUserCollectiblesOwnedDao;
import com.sc.nft.entity.ExportRecords;
import com.sc.nft.entity.TempUserCollectiblesOwned;
import com.sc.nft.entity.vo.UserCollectionExcelVO;
import com.sc.nft.enums.ExportRecordsTypeEnum;
import com.sc.nft.enums.ExportTypeEnum;
import com.sc.nft.enums.ExportTypeStatusEnum;
import com.sc.nft.enums.ReceivedCollectionType;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.TempUserCollectiblesOwnedMapper;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.TempUserCollectiblesOwnedService;
import com.sc.nft.service.UserCollectionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 朱梦晨
 * @Description: 用户拥有的藏品
 * @date 2024-07-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TempUserCollectiblesOwnedServiceImpl implements TempUserCollectiblesOwnedService {


    private final ExportRecordsDao exportRecordsDao;
    private final TempUserCollectiblesOwnedDao tempUserCollectiblesOwnedDao;
    private final UserCollectionService userCollectionService;
    private final TempUserCollectiblesOwnedMapper tempUserCollectiblesOwnedMapper;

    /**
     * 增加用户拥有的藏品导出数据 -> 数据库
     */
    @Override
    public Boolean saveTempUserCollectiblesOwned(String firstTitle, String userTel, Date startTime, Date endTime) {

        List<UserCollectionExcelVO> data = userCollectionService.userCollectionExcel(firstTitle, startTime, endTime, userTel);
        if (CollectionUtils.isEmpty(data)) {
            throw new GlobalRunTimeException("导出数据为空");
        }
        //增加导出记录
        ExportRecords exportRecords = new ExportRecords();
        StringBuilder exportName = new StringBuilder();
        exportName.append(ExportTypeEnum.TEMP_USER_COLLECTIBLES_OWNED.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
        exportRecords.setExportName(exportName.toString());
        exportRecords.setExportType(ExportTypeEnum.TEMP_USER_COLLECTIBLES_OWNED.getCode());
        exportRecords.setStatus(ExportTypeStatusEnum.EXPORT_STATUS_IN.getCode());
        exportRecords.setCreatorId(SysMemberPool.getMember().getId());
        exportRecords.setType(ExportRecordsTypeEnum.ADMIN);
        exportRecords.insert();
        Long exportRecordsId = exportRecords.getId();
        try {
            data.forEach(userCollectionExcelVO -> {
                userCollectionExcelVO.setTypeName(ReceivedCollectionType.getName(userCollectionExcelVO.getType()).getDescription());
                userCollectionExcelVO.setStatusText(userCollectionExcelVO.getStatus());
                userCollectionExcelVO.setOnConsignText(userCollectionExcelVO.getOnConsign());
                userCollectionExcelVO.setFreezingStatusText(userCollectionExcelVO.getFreezingStatus());
            });
            List<TempUserCollectiblesOwned> tempUserCollectiblesOwnedList = BeanUtil.copyToList(data, TempUserCollectiblesOwned.class);
            tempUserCollectiblesOwnedList.forEach(t -> t.setExportRecordsId(exportRecordsId));
            tempUserCollectiblesOwnedMapper.saveBatch(tempUserCollectiblesOwnedList);
        } catch (Exception e) {
            //导出失败
            log.error("用户藏品列表导出失败:", e);
            exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_FAIL.getCode());
            return false;
        }
        exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_SUCCESS.getCode());
        return true;
    }


    /**
     * 下载  根据导出记录Id
     *
     * @param exportRecordsId
     * @return
     */
    @Override
    public List<TempUserCollectiblesOwned> findTempUserCollectiblesOwnedList(Long exportRecordsId) {
        return tempUserCollectiblesOwnedDao.getTempUserCollectiblesOwnedList(exportRecordsId);
    }


}
